<div class="ui large breadcrumb" style="margin-top: 20px;margin-bottom: 15px;">
    <a class="section" onclick="fillSearchText('${groupId}')">${groupId}</a>
    <div class="divider"> /</div>
    <a class="section" onclick="fillSearchText('${artifactId}')">${artifactId}</a>
    <div class="divider"> /</div>
    <div class="active section">详情</div>
</div>

<#if docHtml??>
    <div class="ui ">
        <div class="doc tips">
        </div>
        <div class="doc full" style="display: none">
            ${docHtml}
        </div>
        <div class="ui    small button unfold" onclick="showDocFull();" tabindex="0">
            <i class="angle double down  icon"></i><span class="text">展开显示更多案例...</span>
        </div>
        <script>
            $(function () {
                let docHtml = $(".doc.full").text();
                let docTips = docHtml.slice(0, Math.min(100, docHtml.length))
                $(".doc.tips").text(docTips + "....");
                // code 渲染

                $(".doc.full pre>code").each(function () {
                    hljs.highlightBlock(this);
                })
                if (clickDocButton) {
                    showDocFull();
                    clickDocButton = false;
                }
            });
        </script>
    </div>
</#if>
<#if artifact??>
    <div class="ui message">
        <div class="ui  link large horizontal celled list">
            <#setting url_escaping_charset='utf-8'>
            <#if artifact.officialSite??>
                <a class="item" target="_blank" href="/redirect?site=${artifact.officialSite?url}"><i class="ui icon home"></i>官网</a>
            </#if>
            <#if artifact.docSite??>
                <a class="item" target="_blank" href="/redirect?site=${artifact.docSite?url}"><i class="ui icon file"></i>文档</a>
            </#if>
            <#if artifact.sourceSite??>
                <a class="item" target="_blank" href="/redirect?site=${artifact.sourceSite?url}"><i class="ui icon code"></i>源码</a>
            </#if>
        </div>
        <div style="color: rgba(0,0,0,.6);line-height: 1.6;">${artifact.describe}</div>
    </div>
</#if>
<style>
    .content .snippet-options {
        margin-bottom: 10px;
        display: flex;
        align-items: center;
        background-color: #f7f7f7;
        border: 1px solid #e0e0e0;
        border-radius: 5px;
        padding: 10px;
    }

    .content .left-options {
        display: flex;
        align-items: center;
        flex-grow: 1;
    }

    .content .scope-container {
        display: flex;
        align-items: center;
    }

    .content .style-container {
        display: flex;
        align-items: center;
        margin-left: 15px;
    }

    .content .scope-label {
        margin-right: 5px;
        font-size: 12px;
    }

    .content .style-label {
        font-size: 12px;
    }

    .content .snippet-scope-selector {
        display: inline-block;
        font-size: 12px;
    }

    .content .snippet-variant-selector {
        display: inline-block;
        font-size: 12px;
    }

    .content select {
        padding: 2px;
        border-radius: 3px;
        border: 1px solid #ccc;
    }

    #backToTop {
        transition: opacity 0.3s, visibility 0.3s;
        position: fixed;
        bottom: 20px;
        right: 20px;
        z-index: 1000;
        display: none;
    }

    #backToTop.visible {
        display: block !important;
    }
</style>
<table class="ui version selectable table">
    <tbody>
    <tr>
        <td class="ten wide">版本</td>
        <td class="right aligned">下载</td>
        <td class="right aligned">引用次数</td>
        <td class="right aligned">发布时间</td>
    </tr>
    <#list items as item>
        <tr onclick="doFold($(this))">
            <td>${item.version}</td>
            <td class="right aligned">
                <i class="download link grey icon" data-url="${item.groupId}/${item.artifactId}/${item.version}" style="z-index: 1000;"></i>
            </td>
            <td class="right aligned">${item.attributes.versionCount}</td>
            <td class="right aligned"> ${item.lastModified?number_to_datetime?string("yyyy-MM-dd")!}</td>
        </tr>
        <tr class="content" style="display: none">
            <td colspan="5" style="padding: 0;">
                <div class="ui red attached segment">
                    <div class="ui top attached tabular pointing secondary menu">
                        <a class="item active" data-tab="maven-${item.version}">maven</a>
                        <a class="item" data-tab="gradle-${item.version}">gradle</a>
                    </div>
                    <div class="ui bottom attached tab segment active" data-tab="maven-${item.version}" style="padding-bottom: 0;">
                        <div class="snippet-options">
                            <div class="left-options">
                                <div class="scope-container">
                                    <span class="scope-label">作用域：</span>
                                    <select class="snippet-scope-selector maven-scope" onchange="scopeOnChange('maven','${item.groupId}','${item.artifactId}','${item.version}')">
                                        <option value="compile" selected="">Compile</option>
                                        <option value="test">Test</option>
                                        <option value="provided">Provided</option>
                                        <option value="runtime">Runtime</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                        <textarea class="maven-textarea" rows="7" style="width: 100%;" data-groupid="${item.groupId}" data-artifactid="${item.artifactId}" data-version="${item.version}"
                                  onfocus="areaOnfocus(this,'${item.groupId}','${item.artifactId}','${item.version}');"><dependency>
    <groupId>${item.groupId}</groupId>
    <artifactId>${item.artifactId}</artifactId>
    <version>${item.version}</version>
</dependency></textarea>
                    </div>
                    <div class="ui bottom attached tab segment" data-tab="gradle-${item.version}">
                        <div class="snippet-options">
                            <div class="left-options">
                                <div class="scope-container">
                                    <span class="scope-label">作用域：</span>
                                    <select class="snippet-scope-selector gradle-scope"
                                            onchange="scopeOnChange('gradle','${item.groupId}','${item.artifactId}','${item.version}')">
                                        <option value="compile" selected="">Compile</option>
                                        <option value="test">Test</option>
                                        <option value="provided">Provided</option>
                                        <option value="runtime">Runtime</option>
                                    </select>
                                </div>
                                <div class="style-container">
                                    <span class="style-label">格式：</span>
                                    <select class="snippet-variant-selector gradle-variant"
                                            onchange="scopeOnChange('gradle', '${item.groupId}','${item.artifactId}','${item.version}')">
                                        <option value="Groovy Short" selected="">Groovy Short</option>
                                        <option value="Groovy Long">Groovy Long</option>
                                        <option value="Kotlin">Kotlin</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                        <textarea class="gradle-textarea" rows="6" style="width: 100%" data-groupid="${item.groupId}" data-artifactid="${item.artifactId}" data-version="${item.version}"
                                  onfocus="areaOnfocus(this,'${item.groupId}','${item.artifactId}','${item.version}');">implementation '${item.groupId}:${item.artifactId}:${item.version}'</textarea>
                    </div>
                    <div id="clipboardmsg"></div>
                </div>
            </td>
        </tr>
    </#list>
    <#if items?size==0>
        <div class="error message">没有找到任何记录</div>
    </#if>
    </tbody>
</table>
<div class="ui center aligned basic segment">
    <div class="ui small text muted">
        <a href='https://mvn.coderead.cn/' target="_blank">coderead.cn</a>
    </div>
</div>
<!-- 回到顶部按钮 -->
<div id="backToTop" class="ui circular icon button" title="回到顶部">
    <i class="arrow up icon"></i>
</div>
<script>
    $(function () {
        initVersionUI();

        // 创建回到顶部按钮的点击事件
        $('#backToTop').click(function () {
            $('html, body').animate({scrollTop: 0}, 500);
        });

        // 监听滚动事件，控制回到顶部按钮的显示/隐藏
        $(window).scroll(function () {
            // 获取滚动位置和页面高度信息
            const scrollTop = $(this).scrollTop();
            const windowHeight = $(window).height();

            // 当接近页面底部时显示按钮（距离底部100px以内）
            if (scrollTop > windowHeight) {
                $('#backToTop').addClass('visible');
            } else {
                $('#backToTop').removeClass('visible');
            }
        });

        // download icon
        $('.download,.icon').click(function (event) {
            if (event && event.stopPropagation) {
                event.stopPropagation();
            } else {
                window.event.cancelBubble = true;
            }

            const attr = $(this).attr("data-url");
            if (!attr || !attr.split) {
                return
            }
            const data = attr.split('/');
            let url = "https://repo1.maven.org/maven2/";
            url += data[0].replaceAll('.', '/') + '/';
            url += data[1];
            url += '/';
            url += data[2] + '/' + data[1] + '-' + data[2] + '.jar';
            window.location = url;
        });
    })

    function showDocFull() {
        $(".doc.full").toggle();
        $(".doc.tips").toggle();
        if ($(".doc.full").is(":hidden")) {
            // 是否可空
            $(".button.unfold span.text").text("展开显示更多案例...")
        } else {
            $(".button.unfold span.text").text("收起")
        }
    }


    function download(e) {
        //如果提供了事件对象，则这是一个非IE浏览器
        if (e && e.stopPropagation)
            e.stopPropagation();        //因此它支持W3C的stopPropagation()方法
        else
            window.event.cancelBubble = true;        //否则，我们需要使用IE的方式来取消事件冒泡
    }
</script>
